'use strict';

const Controller = require('egg').Controller;

class MessageController extends Controller {
    async createMessage() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;
        const { body } = ctx.request;
        const { type, content } = body;

        try {
            const result = await service.message.createMessageSrv(userId, targetUserId, type, content);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async setMessageAsRead() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.message.setMessageAsReadSrv(userId, targetUserId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async shieldMessage() {
        const { ctx, service } = this;
        const { messageId } = ctx.params;
        const { body } = ctx.request;
        const { messageType } = body;

        try {
            if (messageType === 1) {
                const [result] = await service.message.shieldSendMessageSrv(messageId);
                if (result) {
                    ctx.successResult(result);
                } else {
                    ctx.errorResult('shield message failed');
                }
            } else if (messageType === 2) {
                const [result] = await service.message.shieldReceiveMessageSrv(messageId);
                if (result) {
                    ctx.successResult(result);
                } else {
                    ctx.errorResult('shield message failed');
                }
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async shieldConversation() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.message.shieldConversationSrv(userId, targetUserId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMessageById() {
        const { ctx, service } = this;
        const { messageId } = ctx.params;

        try {
            const result = await service.message.getMessageByIdSrv(messageId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getChatMessageList() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;
        const { query } = ctx;

        try {
            const result = await service.message.getChatMessageSrv({userId, targetUserId, ...query});
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getUserMessageList() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { query } = ctx;

        try {
            const result = await service.message.getUserMessageListSrv({userId, ...query});
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }
}

module.exports = MessageController;
